//
//  GYComposeTextView.swift
//  Weibo
//
//  Created by vane on 16/8/17.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class GYComposeTextView: UITextView {
    
    override var font: UIFont?{
        didSet{
            placeholderLabel.font = font
        }
    }
    var placeholder: String?{
        didSet{
            placeholderLabel.text = placeholder
        }
    }


    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        backgroundColor = UIColor.whiteColor()
        
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(SCREENW - 10)
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    
    }
    func textViewTextDidChange(){
        placeholderLabel.hidden = self.hasText()
    }
    
    private var placeholderLabel:UILabel = {
        
        let lab = UILabel()
        
        lab.numberOfLines = 0
        
        lab.textColor = UIColor.grayColor()
 
        return lab
        
    }()
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
